package net.ewon.avatar.agent;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageSource;
import net.ewon.avatar.common.pkg.Version;
import net.ewon.config.NaviConfiguration;
import net.ewon.test.TestContextLoader;

import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * /**
 * To run this integration test:
 * 
 * 1. Make sure the mongodb is running
 * 2. Make sure AvatarPackageMgmtService is running
 * 3. Make sure AvatarAgent is running
 * s
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:AvatarAgent-beans.xml", loader=TestContextLoader.class)
@Category({net.ewon.test.WindowsTests.class, net.ewon.test.LinuxTests.class})
public class AvatarAgentServiceIT {
	
	@Resource(name="selfClient")
	private WebClient client;
	
	@Resource(name="pkgMgmtServiceClient")
	private WebClient pkgMgmtServiceClient;
	
	@Resource(name="avatarPackageRepo")
	private AvatarPackageRepo avatarPackageRepo;
	
	@Resource(name="runtimeEnv")
	private RuntimeEnvironment runtimeEnv;
	
	@Resource(name="naviConfiguration")
	private NaviConfiguration naviConfig;
	 
	private RepoTestUtils repoTestUtils;
	
	private String preInstalledPkgName = "PreInstalledPackage";
	private Version preInstalledPkgVersion = Version.parse("1.0.0"); 
	
	private String remoteRepoPkgName="remoteRepoPkg";
	private Version remoteRepoPkgVersion = Version.parse("1.1.0"); 
 
	@Before
	public void setUp() throws Exception { 
		repoTestUtils = new RepoTestUtils(avatarPackageRepo, pkgMgmtServiceClient);
		repoTestUtils.installPackage(preInstalledPkgName, preInstalledPkgVersion);
		client.reset();
		client.resetQuery();
	} 

	@After
	public void tearDown() throws Exception {
		repoTestUtils.removeRepo();
		repoTestUtils.removeEnvironments(runtimeEnv.getRuntimeEnvironmentRoot());
		repoTestUtils.removePackageFromRemoteRepo(remoteRepoPkgName, remoteRepoPkgVersion);
	}

	@Test
	public void testMissingAvatarPackages() { 
		String missingPkgName ="MissingAvatarPackage";
		Version missingVersion = new Version(1,1,1); 		
		AvatarPackage missingAvatarPackage = new AvatarPackage(missingPkgName, missingVersion, null);
   
		AvatarPackage preInstalledPkg = new AvatarPackage(preInstalledPkgName, preInstalledPkgVersion,null);
		
		List<AvatarPackage> avatarPackages = new ArrayList<AvatarPackage>();
		avatarPackages.add(missingAvatarPackage);
		avatarPackages.add(preInstalledPkg); 

		client.path("AvatarAgent/missingAvatarPackages");
		client.type("application/json").accept("application/json");

		List<AvatarPackage> missingPkgs = new ArrayList<AvatarPackage>(client.postAndGetCollection(avatarPackages, AvatarPackage.class, AvatarPackage.class));
		assertEquals(1, missingPkgs.size());
		assertEquals(missingPkgName, missingPkgs.get(0).getName());
		assertEquals(missingVersion.toString(), missingPkgs.get(0).getVersion().getVersion());
		
	}
 
	@Test
	public void testSyncAndCreateLinks() {
		repoTestUtils.uploadPackageToRemoteRepo(remoteRepoPkgName, remoteRepoPkgVersion);
		
		client.path("AvatarAgent/syncAvatarPackage");
		client.type("application/json").accept("application/json");
		client.query("pkgName", remoteRepoPkgName);
		client.query("version", remoteRepoPkgVersion.toString());
		
		AvatarPackageSource source = new AvatarPackageSource(naviConfig.getString("CapeService.host"), naviConfig.getInt("CapeService.port"));
		
		System.out.println("result="+client.post(source, String.class)); 
		
		
		List<AvatarPackage> pkgs = new ArrayList<AvatarPackage>();
		pkgs.add(new AvatarPackage(remoteRepoPkgName, remoteRepoPkgVersion, null));
		client.reset();
		client.resetQuery();
		client.path("AvatarAgent/createLinks");
		client.type("application/json").accept("application/json");
		client.query("envName", "AvatarAgent");
		client.post(pkgs);
	}

}
